package web.servlet.list.web;

import org.apache.commons.beanutils.BeanUtils;
import web.servlet.list.dao.UserDao;
import web.servlet.list.dao.UserDaoImpl;
import web.servlet.list.domin.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;


/**
 * @Author: 18148
 * @Date: 2020-06-17 17:00:16
 */
@WebServlet("/updateUserServlet")
public class UpdateUserServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        Map<String, String[]> map = req.getParameterMap();
        User user=new User();
        try {
            BeanUtils.populate(user,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        UserDao dao=new UserDaoImpl();
        String ids = req.getParameter("id");
        int id = Integer.parseInt(ids);
        int i = dao.update(id, user);
        if(i>0){
            String path = req.getContextPath();
            resp.sendRedirect(path+"/userListServlet");
        }

    }
}
